<%@ page import="com.the8thfold.anton.users.Roles; org.codehaus.groovy.grails.plugins.springsecurity.SpringSecurityUtils; com.the8thfold.anton.ant.AntJobStatus" contentType="text/html;charset=UTF-8" %>
<html>
<head>
    <title>Results</title>
    <meta name="layout" content="main" />
</head>
<body>

<div>
    <h1>Results</h1>
    <hr/>
    <br/>
    <table>

        <tr>
            <th>Run Name</th>
            <th>Job</th>
            <th>Script</th>
            <th>Status</th>
            <th>Created By</th>
            <th>Run By</th>
            <th></th>
        </tr>
        <g:each in="${results}" status="resultsIndex" var="result">
            <g:if test="${!result.isComplete()}">
                <tr class="${resultsIndex%2 ? 'odd' : 'even'}">
                    <td>
                        <g:link controller="result" action="view" id="${result.runResultsId}">${result.name}</g:link>
                    </td>
                    <td>
                        <g:if test="${SpringSecurityUtils.ifAllGranted(Roles.ROLE_JOB_RUN.toString())}">
                            <g:link controller="job" action="view" id="${result.job.jobId}">${result.job.name}</g:link>
                        </g:if>
                        <g:else>${result.job.name}</g:else>
                    </td>
                    <td>
                        <g:if test="${SpringSecurityUtils.ifAllGranted(Roles.ROLE_SCRIPT_ADMIN.toString())}">
                            <g:link controller="script" action="view" id="${result.job.script.scriptId}">${result.job.script.description}</g:link>
                        </g:if>
                        <g:else>${result.job.script.description}</g:else>
                    </td>
                    <td>${result.status}</td>
                    <td title="${result.dateCreated}">${result.createdBy.firstName + " " + result.createdBy.lastName} (<prettytime:display date="${result.dateCreated}" />)</td>

                    <td>
                        <g:if test="${result.runBy}">
                            ${result.runBy.firstName + " " + result.runBy.lastName} (<prettytime:display date="${result.dateRun}" />)
                        </g:if>
                    </td>
                    <td>
                        <div class="toolbar">
                            <nobr>
                                <button ${result.isCreated() && result.job.isActive() ?: 'disabled=true'} ${SpringSecurityUtils.ifAllGranted(Roles.ROLE_JOB_RUN.toString()) ?: 'disabled'} class="btn-text" onclick="document.location.href='${createLink(controller: 'result', action: 'run', id: result.runResultsId )}'">Run</button>
                                <button ${result.isRunning() ?: 'disabled=true'} ${SpringSecurityUtils.ifAllGranted(Roles.ROLE_JOB_RUN.toString()) ?: 'disabled'} class="btn-text" onclick="document.location.href='${createLink(controller: 'result', action: 'stop', id: result.runResultsId )}'">Stop</button>
                                <button ${!result.isRunning() ?: 'disabled=true'} ${SpringSecurityUtils.ifAllGranted(Roles.ROLE_RESULTS_ADMIN.toString()) ?: 'disabled'} class="btn-text" onclick="document.location.href='${createLink(controller: 'result', action: 'delete', id: result.runResultsId )}';">Delete</button>
                            </nobr>
                        </div>
                    </td>
                </tr>
            </g:if>
        </g:each>
    </table>
    <br/><br/><br/>
    <h1>Completed</h1>
    <hr/>
    <br/>
    <table>

        <tr>
            <th>Run Name</th>
            <th>Job</th>
            <th>Script</th>
            <th>Created By</th>
            <th>Run By</th>
            <th></th>
        </tr>
        <g:each in="${results}" status="resultsIndex" var="result">
            <g:if test="${result.isComplete()}">
                <tr class="${resultsIndex%2 ? 'odd' : 'even'}">
                    <td>
                        <g:link controller="result" action="view" id="${result.runResultsId}">${result.name}</g:link>
                    </td>
                    <td>
                        <g:if test="${SpringSecurityUtils.ifAllGranted(Roles.ROLE_JOB_RUN.toString())}">
                            <g:link controller="job" action="view" id="${result.job.jobId}">${result.job.name}</g:link>
                        </g:if>
                        <g:else>${result.job.name}</g:else>
                    </td>
                    <td>
                        <g:if test="${SpringSecurityUtils.ifAllGranted(Roles.ROLE_SCRIPT_ADMIN.toString())}">
                            <g:link controller="script" action="view" id="${result.job.script.scriptId}">${result.job.script.description}</g:link>
                        </g:if>
                        <g:else>${result.job.script.description}</g:else>
                    </td>
                    <td title="${result.dateCreated}">${result.createdBy.firstName + " " + result.createdBy.lastName} (<prettytime:display date="${result.dateCreated}" />)</td>

                    <td>
                        <g:if test="${result.runBy}">
                            ${result.runBy.firstName + " " + result.runBy.lastName} (<prettytime:display date="${result.dateRun}" />)
                        </g:if>
                    </td>
                    <td>
                        <div class="toolbar">
                            <nobr>
                                <button ${!result.isRunning() ?: 'disabled=true'} ${SpringSecurityUtils.ifAllGranted(Roles.ROLE_RESULTS_ADMIN.toString()) ?: 'disabled'} class="btn-text" onclick="if (confirm('Are you sure?')){document.location.href='${createLink(controller: 'result', action: 'delete', id: result.runResultsId )}';}">Delete</button>
                            </nobr>
                        </div>
                    </td>
                </tr>
            </g:if>
        </g:each>
    </table>
</div>

</body>
</html>